package common.core
{
	import common.manager.Resource;
	import common.world.World;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.text.TextField;

	public class Config extends EventDispatcher
	{
		public static const 	INITCONFIGCOMPLET : String = "INITCONFIGCOMPLET";
		
		private static var m_ins : Config
		
		private var m_data : Object;
		
		private var m_configId : String;
		
		public function Config()
		{
			
		}
		
		public static function build(url:String) : void
		{
			Instance.initConfig(url);
		}
		
		/**
		 * 获取配置Config.xml的数据
		 * @return  
		 */		
		public static function data() : Object
		{
			return Instance.getData( );
		}
		
		private function getData() : Object
		{
			return m_data;
		}
		
		/**
		 * 获取配置资源路径
		 * @param id
		 * @return  
		 */		
		public static function getURL(id:String) : String
		{
			return Instance.getData()[id]["url"];
		}
		
		private function initConfig(url:String) : void
		{
			m_configId = url;
			
			Resource.add(url,url);
			
			Resource.start(url,loadConfigComplete,loadConfigProgress);
		}
		
		public function destory() : void
		{
			m_data = null;
			m_configId = null;
		}
		
		private function loadConfigProgress(evt : ProgressEvent) : void
		{
				
		}
		
		private function loadConfigComplete(evt : Event) : void
		{
			Resource.stop(m_configId,loadConfigComplete);
			
			var xml : XML = Resource.getXML(m_configId,true);
		
			m_data = new Object;
			
			for each(var item:XML in xml.elements("Item"))
			{
				var info:Object = new Object;

				info["id"] = item.@id.toString();
				info["url"] = item.@url.toString();
				
				m_data[info["id"]] = info;
			}
			
			Instance.dispatchEvent(new Event(INITCONFIGCOMPLET));
		}
		
		public static function get Instance() : Config
		{
			if(m_ins == null)
			{
				m_ins = new Config;
			}
			
			return m_ins;
		}
	}
}